(function($) {
    var Toolsbar = function(configs) {
        this.opts = $.extend(Toolsbar.defaults, configs, true);
        this.$toolBar = $("#" + Toolsbar.defaults.id);
        this.init();
    };
    window.Toolsbar = Toolsbar;
    Toolsbar.prototype = {
        init: function() {
            this._iefix();	      //解决IE6不支持css fix定位的bug
            this.message = new Toolsbar.Messages(this.opts.messageConfig);
        },
        _iefix:function() {
            var $this = this;
            if (giant.B.msie && giant.B.version == "6.0") {
                this.$toolBar.css({"position":"absolute","z-index":100,"bottom":""});
                var top = $(window).height();
                $this.$toolBar.css("top", top - 28 + "px");
                $(window).scroll(function () {
                    $this.$toolBar.css("top", top - 28 + $(document).scrollTop() + "px");
                });
            }
        },
        addPlugIn: function() {

        },
        removePlugIn: function() {

        },
        updatePlugIn: function() {

        }
    };
    Toolsbar.defaults = {
        id:"toolsbar",
        messageConfig:{
        }
    };
    Toolsbar.Messages = function(configs) {
        this.opts = $.extend({},Toolsbar.Messages.defaults, configs, true);
        this.$message = $("#" + this.opts.id);
        this.$child=this.$message.find("ul.t-message-list");
        this.childIsShow = false;
        this.hasMessage = false;                               //是否已存在未读消息
        this.$sysmsgNum = this.$child.find("em").eq(0);    //系统消息
        this.$smsNum =this.$child.find("em").eq(1);  //短消息
        this.$msgNum =this.$child.find("em").eq(2);  //留言
        this.$msgreplyNum = this.$child.find("em").eq(3); //留言回复
        this.$cmtNum = this.$child.find("em").eq(4);       //评论
        this.$cmtreplyNum = this.$child.find("em").eq(5);       //评论回复
        this.$fqmsgNum = this.$child.find("em").eq(6);
        this.$toltalMessageNum =this.$message.find(".t-r-win span.num");   //评论回复
        this.poptip = null;
        this._init();
    };
    Toolsbar.Messages.prototype = {
        _init:function() {
            var $this = this;
            this._formartPage();
            setInterval(function(){
                if (!$this.hasMessage) {
                    $this._getData();
                }
            },this.opts.timeInterval);
            this._clickBind();
        },
        _formartPage:function(){
             if(parseInt(this.$toltalMessageNum.text())>0){
                this.hasMessage = true;
                this.showTip();
                this.hideTip(5000);
                _flashBg(this.$message.find("a.t-r-win"));
             }
        },
        _clickBind:function(){
            var $this = this;
           $(this.$message.find(".t-r-win")).click(function(event){
               $this.childIsShow?$this.hideChild():$this.showChild();
           });
            $(document).click(function(event){
                if($(event.target).parents(".t-message").attr("id")==$this.id){
                    setTimeout(function(){
                        if($this.childIsShow){
                            $this.hideChild();
                        }
                    },100);
                }
            });
        },
        _getData:function() {
            var $this = this;
            GiantAjax.responseJson({
                crossdomain:true,
                url: Path['domain'] + "/msg/ajaxNewMsgs.do",
                success:function(data) {
                    var total = data.sysmsg + data.sms + data.msg + data.msgreply + data.cmt + data.cmtreply + data.fqmsg;
                    $this.$sysmsgNum.html(data.sysmsg);
                    $this.$smsNum.html(data.sms);
                    $this.$msgNum.html(data.msg);
                    $this.$msgreplyNum.html(data.msgreply);
                    $this.$cmtNum.html(data.cmt);
                    $this.$cmtreplyNum.html(data.cmtreply);
                    $this.$fqmsgNum.html(data.fqmsg);
                    $this.$toltalMessageNum.html(total);
                    if (total > 0) {
                        $this.hasMessage = true;
                        $this.showTip();
                        $this.hideTip(5000);
                        _flashBg($this.$message.find("a.t-r-win"));
                    }
                    data.sysmsg > 0 ? $this.$sysmsgNum.parents("a").addClass("atv") : $this.$sysmsgNum.parents("a").removeClass("atv");
                    data.sms > 0 ? $this.$smsNum.parents("a").addClass("atv") : $this.$smsNum.parents("a").removeClass("atv");
                    data.msg > 0 ? $this.$msgNum.parents("a").addClass("atv") : $this.$msgNum.parents("a").removeClass("atv");
                    data.msgreply > 0 ? $this.$msgreplyNum.parents("a").addClass("atv") : $this.$msgreplyNum.parents("a").removeClass("atv");
                    data.cmt > 0 ? $this.$cmtNum.parents("a").addClass("atv") : $this.$cmtNum.parents("a").removeClass("atv");
                    data.cmtreply > 0 ? $this.$cmtreplyNum.parents("a").addClass("atv") : $this.$cmtreplyNum.parents("a").removeClass("atv");
                    data.cmtreply > 0 ? $this.$cmtreplyNum.parents("a").addClass("atv") : $this.$cmtreplyNum.parents("a").removeClass("atv");
                    data.fqmsg > 0 ? $this.$fqmsgNum.parents("a").addClass("atv") : $this.$fqmsgNum.parents("a").removeClass("atv");
                }
            });
        },
        showTip:function(content) {
            content = content||"您有新的消息，请注意查收！";
            Toolsbar.PopTip.hideAllTip();
            this.poptip = new Toolsbar.PopTip({source:"#" + this.opts.id,content:content});
            this.poptip.show();
        },
        hideTip:function(time) {
            time = time||0;

            if(this.poptip){
                this.poptip.hide(time);
            }
        },
        showChild:function(){
            Toolsbar.PopTip.hideAllTip();
            this.$child.animate({"top":"-176px","opacity":"show"}, "normal");
            this.childIsShow = true;
        },
        hideChild:function(){
            this.$child.animate({"top":"0px","opacity":"hide"}, "normal");
            this.childIsShow = false; 
        }
    };
    Toolsbar.Messages.defaults = {
        id:"toolinfo" ,
        timeInterval:30000
    };
    /**
     * 用于信息提示
     * */
    Toolsbar.PopTip = function(options) {
        this.opts = $.extend({}, Toolsbar.PopTip.defaults, options);
        this.id = Toolsbar.PopTip.PREFIX + Toolsbar.PopTip.AUTO_ID;
        this.$tip = null;
        Toolsbar.PopTip.AUTO_ID++;
        Toolsbar.PopTip.instanceArr.push(this);
        this._getTemp();
    };
    Toolsbar.PopTip.PREFIX = 'toolsbar-poptip-';    //默认的tipId 前缀
    Toolsbar.PopTip.AUTO_ID = 1000;           //tipId 生成页面唯一ID
    /**
     *静态属性，存放页面生命周期内生成的所有poptip示例，为数组形式
     */
    Toolsbar.PopTip.instanceArr = [];
    /**
     *静态方法，隐藏页面上所有的错误提示
     */
    Toolsbar.PopTip.hideAllTip = function() {
        $.each(Toolsbar.PopTip.instanceArr, function(i, n) {
            n.hide();
        });
    };
    /**
     *静态方法，移除页面上所有的错误提示的dom
     */
    Toolsbar.PopTip.removeAllTip = function() {
        $.each(Toolsbar.PopTip.instanceArr, function(i, n) {
            n.remove();
            delete n;
        });
    };
    Toolsbar.PopTip.prototype = {
        /**
         * 显示该tip
         */
        show:function() {
            this.$tip.css("top", "-" + (this.$tip.outerHeight(true) + 50) + "px");
            this.$tip.animate({"top":"+=40px","opacity":"show"}, "normal");
        },
        /**
         * 隐藏该tip
         */
        hide:function(time) {
            var $this = this;
            time = time || 0;
            setTimeout(function() {
                $this.$tip.animate({"top":"-=40px","opacity":"toggle"}, "normal",function(){
                   $this.remove(); 
                });
            }, time);
        },
        /**
         * 移除该tip的dom
         */
        remove:function() {
            this.$tip.remove();
        },
        /**
         * 设置tip内的内容
         */
        setContent:function(str) {
            this.$tip.find(".cont").html(str);
        },
        /**
         * 判断该tip是否在页面上可见，若可见，返回true,否则返回false
         */
        isVisible:function() {
            if ($("#" + this.id + ":visible").size() > 0) {
                return true;
            }
            return false;
        },
        _getTemp:function() {
            var str = new StringBuilder();
            str.append('<div class="tip warning w160 none" id="' + this.id + '">');
            str.append('<div class="cont">');
            if (this.opts.hasName)
                str.append('<p><a title="用户名" href="#">' + this.opts.name + '</a> 说：</p>');
            str.append('<p>' + this.opts.content + '</p>');
            str.append('</div><div class="arrow_down"/></div>');
            $(str.toString()).appendTo($(this.opts.source));
            this.$tip = $("#" + this.id);
        }
    };
    /**
     * @default
     * <pre>
     * width==>poptip宽度,设为auto则宽度自适应<br />
     * direction==>tip相对与源显示的位置，有右侧和上面两种，默认为右侧"right"
     * tipType==>tip显示方式，有两种方式，一种为错误警告，一种为信息提示。默认为错误警告
     * source==>tip指向的dom源。
     * content==>tip中的内容。
     * </pre>
     */
    Toolsbar.PopTip.defaults = {
        hasName:false,
        name:"",
        source:"input",
        content:""
    };
    //背景闪烁变色
    function _flashBg($obj) {
        var i = 0;
        var interval = setInterval(function() {
            $obj.toggleClass("active");
            i++;
            if (i == 7) {
                clearInterval(interval);
            }
        }, 700);
    }
})(jQuery);